﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using PlanningGIS.Framework;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
//using ESRI.ArcGIS.TOCControl;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    ///  作用：收缩所有图层
    /// </summary>
    internal  class AllLayersCollapsed:BaseCommand 
    {
        IApplication m_Application = null;
        public AllLayersCollapsed()
        {
            base.m_caption = "收缩所有图层";
            base.m_toolTip = base.Caption;
        }

        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        public override void OnClick()
        {   
            for (int i = 0; i < (m_Application.Document as IMxDocument).ActiveView.FocusMap.LayerCount; i++)
            {
                if ((m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layer(i) is IGroupLayer)
                {
                    IGroupLayer pCmpLyr =
                         (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layer(i) as IGroupLayer;
                    pCmpLyr.Expanded = false;
                }
                else if ( (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layer(i) is ICompositeLayer2)
                {
                    ICompositeLayer2 pCmpLyr =
                         (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layer(i) as ICompositeLayer2;
                    pCmpLyr.Expanded = false;
                }
                else if ( (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layer(i) is IFeatureLayer)
                {
                    ILegendInfo pLegendInfo =
                         (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layer(i) as ILegendInfo;
                    for (int j = 0; j < pLegendInfo.LegendGroupCount; j++)
                    {
                        ILegendGroup pLegendGroup =
                            pLegendInfo.get_LegendGroup(j);
                        pLegendGroup.Visible = false;
                    }
                }
            }
            (m_Application as IFramework).TOCControl.Update();          
            
        }

        public override bool Enabled
        {
            get
            {
                return (m_Application as IFramework).IsMapCtrlactive;
            }
        }
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
    
}
